#include "folderfavoritesitem.h"
#include <QDataStream>
#include "favoritescreator.h"
#include <QIcon>
#include "../terminalglobals.h"

using namespace ExecTerminalImplNS;

const bool I_AM_FOLDER = true;

FolderFavoritesItem::FolderFavoritesItem(const QString & aname, FolderFavoritesItem * prnt): AbstractFavoritesItem(aname, prnt)
{

}

FolderFavoritesItem::FolderFavoritesItem(FolderFavoritesItem * prnt): AbstractFavoritesItem(prnt)
{

}

FolderFavoritesItem::~FolderFavoritesItem()
{
    qDeleteAll(theChildren);
}

bool FolderFavoritesItem::isFolder()const
{
    return I_AM_FOLDER;
}

void FolderFavoritesItem::appendChild(AbstractFavoritesItem *child)
{
    theChildren.append(child);
}

AbstractFavoritesItem * FolderFavoritesItem::child(int number)
{
    if (number < 0 || number >= theChildren.count())
    {
        return 0;
    }
    return theChildren.at(number);
}

int FolderFavoritesItem::childCount()const
{
    return theChildren.count();
}

void FolderFavoritesItem::readFromDataStream(QDataStream & ds)
{
    AbstractFavoritesItem::readFromDataStream(ds);
    qint32 chldCount = -1;
    ds >> chldCount; 
    for (int i = 0; i < chldCount; ++i)
    {
        bool isChildFolder = false;
        ds >> isChildFolder;
        AbstractFavoritesItem * child = FavoritesCreator::createFavoritesItem(isChildFolder, this);
        ds >> *child;
        theChildren.append(child);
    }
}

void FolderFavoritesItem::writeToDataStream(QDataStream & ds)const
{
    AbstractFavoritesItem::writeToDataStream(ds);
    ds << static_cast<qint32>(theChildren.count());
    foreach(AbstractFavoritesItem * child, theChildren)
    {
        ds << *child;
    }
}


QIcon FolderFavoritesItem::provideIcon()const
{
    QIcon icon(pixmapForPath(":Resources/ico_folder.png"));
    icon.addPixmap(pixmapForPath(":Resources/ico_folder_open.png"), QIcon::Normal, QIcon::On);
    return icon;
}

